import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.LinkedList;

import static javax.swing.SwingConstants.RIGHT;

/**
 * Created by ospip on 2017/11/7.
 * Copyright (c) 2017 LLAP. All rights reserved.
 */
public class AverageFrame extends JFrame {
    private ArrayList<ValueGroup> vg;
    private JButton btnAdd, btnRemove, btnConfirm;
    public AverageFrame() {
        super("平均值");
        try {
            UIManager.setLookAndFeel (UIManager.getSystemLookAndFeelClassName ());
        }
        catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        this.setSize(830, 310);
        this.setLayout(null);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(this.getOwner());
        btnAdd = new JButton("+");
        btnRemove = new JButton("-");
        btnConfirm = new JButton("计算");

        btnAdd.setSize(150, 80);
        btnAdd.setLocation(510, 0);
        btnRemove.setSize(150, 80);
        btnRemove.setLocation(660, 0);
        btnConfirm.setSize(300, 80);
        btnConfirm.setLocation(510, 80);
        btnConfirm.addActionListener((e) -> {
        });
        vg = new ArrayList<>();

        this.add(btnAdd);
        this.add(btnRemove);
        this.add(btnConfirm);
        System.out.println(this.getHeight());
        System.out.println(this.getSize().height);
        addItem();
        addItem();
        addItem();
        addItem();
        btnAdd.addActionListener((e) -> {
            addItem();
            this.repaint();
            this.changeHeight(this.getHeight() + 60);
        });
        btnRemove.addActionListener((e) -> {
            if (vg.size() == 4) return;
            removeItem();
            this.repaint();
            this.changeHeight(this.getHeight() - 60);
        });
        btnConfirm.addActionListener((e) -> {
            cal();
        });
    }

    public void addItem() {
        vg.add(new ValueGroup(0, 60 * vg.size()));
        this.add(vg.get(vg.size() - 1));
    }

    public void removeItem() {
        this.remove(vg.get(vg.size() - 1));
        vg.remove(vg.size() - 1);
    }

    public void changeHeight(int h) {
        int width = this.getWidth();
        int step = (h - this.getHeight()) / 25;
        if (step == 0) {
            this.setSize(width, h);
            return;
        }
        Timer timer = new Timer(1, null);
        timer.addActionListener((e) -> {
            this.setSize(width, this.getHeight() + step);
            if ((step > 0 && this.getHeight() >= h) || (step < 0 && this.getHeight() <= h)) {
                this.setSize(width, h);
                timer.stop();
            }
        });
        timer.start();
    }

    public void cal() {
        double sum = 0;
        for (ValueGroup i : vg) {
            sum += i.getValue();
        }
        JOptionPane.showMessageDialog(null, (sum / vg.size()) + "");
    }

    public static void main(String[] args) {
        new AverageFrame().setVisible(true);
    }
}
class ValueGroup extends JPanel {
    private JTextField value, weight;
    private JLabel lValue = new JLabel("值", RIGHT);
    private JLabel lWeight = new JLabel("权重", RIGHT);

    public ValueGroup(int x, int y) {
        super(null);
        this.setSize(500, 60);
        value = new JTextField();
        value.setSize(200, 40);
        value.setLocation(50, 10);
        weight = new JTextField("1");
        weight.setSize(200, 40);
        weight.setLocation(300, 10);
        lValue.setSize(50, 40);
        lWeight.setSize(50, 40);
        lValue.setLocation(0, 10);
        lWeight.setLocation(250, 10);
        this.add(lValue);
        this.add(lWeight);
        this.add(value);
        this.add(weight);
        this.setLocation(x, y);
    }

    public double getValue() {
        double v = 0, w = 0;
        try {
            v = Double.parseDouble(value.getText());
            w = Double.parseDouble(weight.getText());
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return v * w;
    }
}